package com.rxh.service;

import com.rxh.cache.CoreAcquirerAccountCache;
import com.rxh.exception.PayException;
import com.rxh.pojo.Result;
import com.rxh.pojo.core.CoreAcquirerAccount;
import com.rxh.pojo.core.CoreOrderChange;
import com.rxh.pojo.cross.BankResult;
import com.rxh.pojo.payment.Refund;
import com.rxh.utils.HttpClientUtils;
import com.rxh.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: 王德明
 * Date: 2018/10/19
 * Time: 16:32
 * Project: Management_new
 * Package: com.rxh.service
 */
@Service
public class RefundService {

    @Autowired
    private CoreAcquirerAccountCache coreAcquirerAccountCache;
    private Logger logger = LoggerFactory.getLogger(getClass());

    public Result<CoreOrderChange> getResultMessage(Refund refund) throws PayException {
        //返回参数集合
        Result<CoreOrderChange> result = new Result<>();
        //收单账户验证
        CoreAcquirerAccount coreAcquirerAccount = coreAcquirerAccountCache.getById(refund.getOrder().getAccountId());
        refund.setCoreAcquirerAccount(coreAcquirerAccount);
        if (refund.getCoreOrderChange().getType() == CoreOrderChange.TYPE_PARTIAL_REFUND) {
            refund.setChannelOrderInfo(null);
        }
        String content = HttpClientUtils.doPostJson(HttpClientUtils.getHttpClient(), refund.getCoreAcquirer().getUrl() + "/refund", JsonUtils.objectToJson(refund));
        // String content = HttpClientUtils.doPostJson(HttpClientUtils.getHttpClient(), "http://192.168.1.23:8010/cross-three/refund", JsonUtils.objectToJson(refund));
        // BankResult bankResult1 = new BankResult();
        // bankResult1.setStatus(SystemConstant.CHANGE_BANK_STATUS_APPLICATION_SUCCESS);
        // String content = JsonUtils.objectToJson(bankResult1);
        logger.info("return :" + content);
        BankResult bankResult = JsonUtils.jsonToPojo(content, BankResult.class);
        //结果处理
        if (bankResult == null) {
            result.setCode(Result.FAIL);
            result.setMsg("退款失败: 发送接口超时,请稍后再试");
        } else {
            result.setCode(Result.SUCCESS);
            result.setMsg(bankResult.getBankResult());
            CoreOrderChange coreOrderChange = refund.getCoreOrderChange();
            coreOrderChange.setExceptionBankId(bankResult.getBankRefundId());
            coreOrderChange.setRemark(bankResult.getBankResult());
            switch (bankResult.getStatus()) {
                //退款成功
                case BankResult.SUCCESS:
                    coreOrderChange.setStatus(CoreOrderChange.STATUS_SUCCESS);
                    break;
                //退款失败
                case BankResult.FAIL:
                    coreOrderChange.setStatus(CoreOrderChange.STATUS_FAIL);
                    break;
                //退款超时
                case BankResult.TIMEOUT:
                    result.setCode(Result.FAIL);
                    break;
                // 非直连的需要申请，此状态表示申请成功
                case BankResult.PROCESSING:
                    coreOrderChange.setStatus(CoreOrderChange.STATUS_PROCESSING);
                    break;
                default:
                    break;
            }
            result.setData(coreOrderChange);
        }
        return result;
    }
}
